layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var brandId = $("#brandId").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/item/jsonList',
        where: { brandId: brandId},
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type:'checkbox'},
            {field: 'title',minWidth:120, title: '商品名称',align:'center'},
            {field: 'brandName',minWidth:120, title: '商品分类',align:'center'},
            {field: 'prePrice',minWidth:120, title: '现价',align:'center'},
            {field: 'origPrice',minWidth:120, title: '原价',align:'center'},
            {field: 'profit',minWidth:120, title: '利润',align:'center'},
            {field: 'hotStatus',minWidth:170, title: '状态', align:'center', templet:function(d){
                if(d.hotStatus=="2"){
                    return '<input class="editStatus" checked="true" data-id="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-event="editStatus" type="checkbox">';
                }else{
                    return '<input class="editStatus" name="open" data-id="'+d.id+'" lay-skin="switch" lay-filter="switchTest" lay-event="editStatus" type="checkbox">';
                }
            }},
            {field: 'id',minWidth:120, title: '商品链接',align:'center', templet:function(d){
                return '<input class="editStatus" type="text" value="/phone/product/'+d.id+'?">';
            }},
            {field: 'createTime',minWidth:170, title: '创建时间', align:'center', templet:function(d){
                return d.createTime;
            }},
            {title: '操作',width:170,align:"center", templet:function(d){
                return '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>\
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                title: $("#title").val(),
                brandId: $("#brandId").val(),
                brandName: $("#brandName").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        var brandId = $("#brandId").val();
        if(params==null||params==""){
            title="添加"
            params = "?brandId="+brandId;
        }else {
            params = params + "&brandId="+brandId;
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            content : "/item/selectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }
    $(".add_btn").click(function(){
        addEntity('');
    });

    $(".accept_btn").click(function(){
        layer.confirm('是否同步商品',{icon:3, title:'提示信息'},function(index){
            $.get("/item/getDdcAcceptProduct","",function(result){
                if(1==result.returnInfo.state){
                    tableIns.reload();
                    layer.close(index);
                }else{
                    top.layer.msg(result.returnInfo.errorMsg);
                }
            })
        });
    });

    $(".add_price_btn").click(function(){
        // 获取所有的ids
        var checkStatus = layui.table.checkStatus('entityList').data;

        if(checkStatus.length == 0){
            layer.alert("请选择商品！")
            return false;
        }
        var ids = [];
        $.each(checkStatus,function(i,v){
            ids.push(v.id);
        });
        console.log(ids.join(","));
        var index = layui.layer.open({
            title :"商品加价",
            type : 2,
            content : "/item/selectPriceEntity?ids="+ids.join(","),
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                // layui.layer.full(index);
            }
        });
    });

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除商品【'+data.title+'】吗？',{icon:3, title:'提示信息'},function(index){
                $.get("/item/deleteEntity",{
                    "id":data.id,
                    "_method":"DELETE"
                },function(result){
                    if(1==result.returnInfo.state){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        top.layer.msg(result.returnInfo.errorMsg);
                    }
                })
            });
        }else if(layEvent === 'list'){
            //window.location.href = "/activity/selectEntity"+"?itemId="+data.id;
            addActivity("?itemId="+data.id);
        }
    });

    form.on('switch(switchTest)', function(obj){
        // 获取当前控件
        var id = $(this).attr("data-id");
        console.log(id);
        var status = "1";
        if (this.checked){
            status = "2";
        }
        $.get("/item/updateHotStatus",{
            "id":id,
            "status":status,
            "_method":"DELETE"
        },function(result){
            if(1==result.returnInfo.state){
                tableIns.reload();
            }else{
                top.layer.msg(result.returnInfo.errorMsg);
            }
        })
    });

    function addActivity(params){
        var index = layui.layer.open({
            title :"发布",
            type : 2,
            content : "/activity/selectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }

})